<template>
  <view>
    <view class="goodsInfo-box">
      <view class="popupHeader">
        <uni-icons type="closeempty" size="20" @click="closePopup()"></uni-icons>
      </view>
      <view class="address-box" @click="goChooseAddress()">
        <uni-icons type='location' size="28"></uni-icons>
        <view class="addressInfo" v-if="addressInfo">
          <view class="mainInfo">
            {{addressInfo.recipientName}} {{addressInfo.phoneNumber}}
          </view>
          <view class="addressDetail">
            {{addressInfo.privince + addressInfo.city + addressInfo.county + addressInfo.detailAddress}}
          </view>
        </view>
        <view class="default" v-else>
          请添加收货地址
        </view>
        <uni-icons type="right" size="16" color="#ddd"></uni-icons>
      </view>
      <view class="goods-container">
        <view class="goods-detail">
          <view class="image">
            <image :src="goodsDetail.colorVOS[active1].colorImageUrl" mode=""></image>
          </view>
          <view class="priceInfo">
            <view class="price">
              <text class="name">积分</text>
              <text class="integralValue">{{priceValue | tofixed}}</text>
              <text class="devide">|</text>
              <text class="cashValue">￥{{cashValue | tofixed}}</text>
            </view>
            <view class="stock">
              剩余{{goodsDetail.stock}}件
            </view>
            <uni-number-box min="1" @change="handleChange()" :value="goodsCount"
              :max="goodsDetail.stock"></uni-number-box>
          </view>
        </view>
        <scroll-view class="type-box" scroll-y="true">
          <view class="type-container">
            <view class="type-title">
              颜色（{{goodsDetail.colorVOS.length}}）
            </view>
            <view class="type-list">
              <block v-for="(item,index) in goodsDetail.colorVOS" :key='index'>
                <view :class="['type-item',active1===index?'goodsActive':'']" @click="changeType1(index)">
                  <view class="image">
                    <image :src="item.colorImageUrl" mode=""></image>
                  </view>
                  <view class="type-name">
                    {{item.description}}
                  </view>
                </view>
              </block>
            </view>
          </view>
          <view class="type-container" v-if="goodsDetail.size">
            <view class="type-title">
              尺码
            </view>
            <view class="type-list">
              <block v-for="(item,index) in goodsDetail.size" :key='index'>
                <view :class="['type-item',active2===index?'goodsActive':'']" style="padding:0 30rpx ;"
                  @click="changeType2(index)">
                  <view class="type-name" style="margin-left: 0;">
                    {{item}}
                  </view>
                </view>
              </block>
            </view>
          </view>
        </scroll-view>
      </view>
    </view>
    <view class="footer">
      <view class="buy" @click="buy()">
        立即购买
      </view>
    </view>

  </view>
</template>
<script>
  import {
    mapState
  } from 'vuex'
  export default {
    props: {
      addressInfo: Object,
      goodsDetail: Object,
      goodsCount: Number
    },
    data() {
      return {
        active1: 0,
        active2: 0,
      }
    },
    methods: {
      goChooseAddress() {
        uni.navigateTo({
          url: '/subpkg/chooseAddress/chooseAddress'
        })
      },
      closePopup() {
        this.$emit('closePopup')
      },
      handleChange(e) {
        this.$emit('changeCount', e)
      },
      changeType1(i) {
        this.active1 = i
      },
      changeType2(i) {
        this.active2 = i
      },
      async buy() {
        if(this.userInfo.integral < this.priceValue * this.goodsCount) return uni.$showMsg('您的积分不足以买下这件商品！')
        let size = this.goodsDetail.size ? this.goodsDetail.size[this.active2] : ''
        await uni.$http.post('/users/order/payment', {
          userId: this.userInfo.studentId,
          productId: this.goodsDetail.productId,
          addressId: this.addressInfo.addressId,
          quantity: this.goodsCount,
          points: this.priceValue,
          cash: this.cashValue,
          size: size,
          color: this.goodsDetail.colorVOS[this.active1].description,
        })
        uni.$showMsg('购买成功')
        this.closePopup()
      }
    },
    computed: {
      ...mapState('my_user', ['userInfo']),
      cashValue() {
        return this.goodsDetail.cashPrice * this.goodsCount
      },
      priceValue() {
        return this.goodsDetail.price * this.goodsCount
      }
    },
    filters: {
      tofixed(num) {
        return Number(num).toFixed(2)
      }
    },
  }
</script>
<style lang="scss">
  .goodsInfo-box {
    height: 1500rpx;
    background-color: #f1f1f1;
    border-radius: 20rpx 20rpx 0 0;
    overflow: hidden;

    .popupHeader {
      height: 50rpx;
      padding: 10rpx;
      background-color: #fff;
      text-align: right;
      line-height: 50rpx;
    }

    .address-box {
      display: flex;
      padding: 10rpx 20rpx;
      padding-bottom: 30rpx;
      background-color: #fff;

      .addressInfo {
        margin-left: 10rpx;

        .mainInfo {
          display: flex;
          align-items: center;
          width: 600rpx;
          height: 60rpx;
          font-size: 32rpx;
          font-weight: 700;
        }

        .addressDetail {
          font-size: 28rpx;
          margin-top: 6rpx;
          color: #555;
        }
      }

      .default {
        width: 600rpx;
        font-size: 36rpx;
        font-weight: 700;
      }
    }

    .goods-container {
      display: flex;
      flex-direction: column;
      margin-top: 20rpx;
      padding: 20rpx 30rpx;
      background-color: #fff;

      .goods-detail {
        display: flex;

        .image {
          width: 170rpx;
          height: 170rpx;
          border-radius: 20rpx;
          overflow: hidden;

          image {
            width: 100%;
            height: 100%;
          }
        }

        .priceInfo {
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          margin-left: 30rpx;

          .price {
            color: #b6eaae;
            font-weight: 700;

            .name {
              font-size: 28rpx;
            }

            .integralValue {
              margin-left: 6rpx;
              font-size: 50rpx;
            }

            .devide {
              font-size: 24rpx;
              margin: 0 20rpx;
            }

            .cashValue {
              font-size: 32rpx;
            }
          }

          .stock {
            width: fit-content;
            padding: 6rpx 20rpx;
            border-radius: 6rpx;
            background-color: #b6eaae;
            color: #fff;
            font-size: 26rpx;
          }
        }
      }
    }

    .type-box {
      height: 1000rpx;
      background-color: #fff;

      .type-container {
        margin-top: 50rpx;

        .type-title {
          font-size: 32rpx;
        }

        .type-list {
          display: flex;
          flex-wrap: wrap;

          .type-item {
            display: flex;
            width: fit-content;
            height: 50rpx;
            margin-top: 30rpx;
            margin-right: 30rpx;
            padding-right: 40rpx;
            border: 1rpx solid #fff;
            border-radius: 10rpx;
            background-color: #f5f5f5;
            color: #555;
            overflow: hidden;

            .image {
              width: 50rpx;
              height: 50rpx;

              image {
                width: 100%;
                height: 100%;
              }
            }

            .type-name {
              margin-left: 20rpx;
              font-size: 26rpx;
              line-height: 50rpx;
            }

            &.goodsActive {
              border: 1rpx solid #b6eaae;
              color: #b6eaae;
            }
          }
        }
      }
    }

  }

  .footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    height: 100rpx;
    padding: 20rpx 30rpx;
    border-top: 1rpx solid #e9e9e9;
    background-color: #fff;
    z-index: 999;

    .buy {
      width: 90%;
      height: 80rpx;
      border-radius: 6rpx;
      text-align: center;
      line-height: 80rpx;
      background-color: #b6eaae;
      color: #fff;
      font-size: 34rpx;
    }
  }
</style>